package com.skycong.hello77.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.*;

import com.skycong.hello77.R;
import com.skycong.hello77.activity.personhome.PersonHomeActivity;
import com.skycong.hello77.service.UserService;
import com.skycong.hello77.util.Result;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class Login1 extends Activity implements View.OnClickListener{

    private static final String TAG = "android.app.Activity";
    private UserService userService;
    public TextView login;
    private EditText username;
    private EditText password;
    private ImageView imgCheckPwd; //查看密码
    private boolean defalseShow = false;
    private TextView forgetpassword;//忘记密码
    private ImageView img_back;//头部的返回键
    SharedPreferences.Editor editor;
    SharedPreferences sharedPreferences;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userService = new UserService(this);
        //强制在住Activity中执行网络操作
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.activity_login1);

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (TextView) findViewById(R.id.login);
        imgCheckPwd  = (ImageView) findViewById(R.id.img_password_e);
        forgetpassword  = (TextView) findViewById(R.id.forget);
        img_back = (ImageView) findViewById(R.id.img_back);
        checkBox = (CheckBox) findViewById(R.id.checkbox1);

        sharedPreferences = getSharedPreferences("savepasswoerd",MODE_PRIVATE);//初始化对象
        editor = sharedPreferences.edit();//开始编辑
        //第二次进入登录界面，判断有没有保存成功
        String userEmail = sharedPreferences.getString("userEamil","");
        String userPassword = sharedPreferences.getString("userPassword","");
        Log.i("save",userEmail);
        Log.i("save",userPassword);
        //没保存，记住密码就不勾选
        if(userEmail==null && userPassword == null){
            checkBox.setChecked(false);
        }
        imgCheckPwd.setOnClickListener(this);
        forgetpassword.setOnClickListener(this);
        login.setOnClickListener(this);
          //隐藏头部的返回按钮
        hideBack(img_back.GONE);
      /*
      * 获取输入的文本框中的用户名
      * 获取输入的密码
      * */
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id==R.id.img_password_e){
            isShowPassword();//查看密码
        }else if(id==R.id.forget){
            forgetPassword();//忘记密码
        }else if(id==R.id.login){
            toLoginUser();//登录
        }
    }
    /**
     * 处理登录的具体业务
     * 发送 http请求到Android server
     *
     * @return true if login success
     */
    private void toLoginUser() {
        System.out.println("进入到login窗口了" +
                "");
         //调用服务里面的登录方法
        MultiValueMap<String,String > map = new LinkedMultiValueMap<>();
        map.add("email",username.getText().toString());
        map.add("password",password.getText().toString());
        Result result = null;

        try {
            result = userService.loginRequest(map);
        }catch (Exception e){
            Log.i("AAA", e.getMessage());
            Toast.makeText(Login1.this, "连接超时，请检查网络",Toast.LENGTH_LONG).show();
        }
        if (result.getStatus()==200) {
            //记住密码
            if(checkBox.isChecked()) {
                //保存user邮箱和密码
                editor.putString("userEamil", username.getText().toString().trim());
                editor.putString("userPassword", password.getText().toString().toString().trim());
                Log.i("isChecked","勾选了记住密码");
                editor.commit();
            }else{
                editor.remove("userEamil");
                editor.remove("userPassword");
                editor.commit();
            }
                //跳转并传递参数
                Toast.makeText(Login1.this, "登录成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.putExtra("data", username.getText().toString());
                intent.setClass(Login1.this, PersonHomeActivity.class);
                Login1.this.startActivity(intent);

        }else{
            Toast.makeText(Login1.this, "账号或密码错误",Toast.LENGTH_LONG).show();
        }
    }
    /**
     * 查看密码
     */
    private void isShowPassword() {
        if (defalseShow) {
            defalseShow = false;
            imgCheckPwd.setImageResource(R.mipmap.ic_login_close);
            //隐藏密码
            password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            defalseShow = true;
            imgCheckPwd.setImageResource(R.mipmap.ic_login_check);
            //password.setInputType(InputType.TYPE_CLASS_TEXT);
            //显示密码
            password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }

    }

    /**
     * 忘记密码
     * */
    public  void forgetPassword(){
        //跳到找回密码
        Intent intent = new Intent(Login1.this,ForgetActivity.class);
        Login1.this.startActivity(intent);
    }
    /**
     * 隐藏头部的返回按钮
     * */
    public void hideBack(int visibility){
        img_back.setVisibility(visibility);
    }
    /**
     * check账号和密码的格式
     * */
    public void checkFormat(){
          if(username.getText().toString().length()==0 || password.getText().toString()==null){

              Toast.makeText(Login1.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
              return;
          }

    }




}
